<template>
	<view class="order-list" :class="themeName">
		<!-- Tabs -->
		<navbar title="客户订单"></navbar>
		<u-tabs :list="tabsList" :is-scroll="false" :height="80" active-color="rgba(64, 115, 250, 1)"
			:bar-style="{ top: '100%' }" :current="tabsIndex" @change="changeCurrentTab" />

		<!-- Order Lists -->
		<view class="order-item">
			<view class="order-item-box">
				<mescroll-uni ref="mescrollRef" :fixed="false" :up="{
				auto: false,
				noMoreSize: 3,
				empty: {
					icon: $getImageUri('resource/image/shopapi/uniapp/empty/order.png'),
					tip: $zhTran('暂无订单~'),
					fixed: true
				}
			}" :bottom="0" :down="{ auto: false }" @init="mescrollInit" @down="downCallback" @up="upCallback">
					<block v-for="(tabItem, tabIndex) in tabsList" :key="tabItem['sign']">
						<view class="order-contain" v-if="currentTab['sign'] === tabItem['sign']">
							<view class="order" v-for="orderItem in tabsList[tabIndex].list" :key="orderItem.id">
								<!-- Order Header -->
								<view class="order-header p-t-20">

									<view class="flex">
										<view class="order-sn">{{$zhTran('订单编号:')}}{{ orderItem.sn }}</view>
										<view :class="[
										'order-status',
										`order-status--${orderStatus[orderItem.order_status]['style']}`
									]">{{ orderStatus[orderItem.order_status]['name'] }} </view>
									</view>
								</view>

								<!-- Order Main -->
								<view class="order-main" @tap="goOrderDetail(orderItem)">
									<goods-card v-for="(goodsItem, goodsIndex) in orderItem.order_goods"
										:key="goodsIndex" shape="rectangle" :name="goodsItem.goods_name"
										:image="goodsItem.goods_image"
										:contain-style="{ 'border-raius': 0, 'height': '330rpx' }"
										:image-style="{ 'width': '180rpx', 'height': '180rpx' }">
										<view class="flex-1" slot="default">
											<view class="xs m-t-10" v-if="order_extend.show_order_list_goods_tag">
												<u-image width="80rpx" height="40rpx" :src="goodsItem.tag_desc"
													v-if="goodsItem.tag_desc != ''" />
											</view>
											<text class="muted xs m-t-10">{{ goodsItem.spec_value_str }}</text>
											<view class="xs m-t-10" v-if="order_extend.show_order_list_goods_code"
												:style="{
											color: order_extend.order_list_goods_code_color
										}">{{$zhTran('编码：')}}{{ goodsItem.code == null ? '' : goodsItem.code }}</view>
											<view class="xs m-t-10" v-if="order_extend.show_order_list_goods_model"
												:style="{
											color: order_extend.order_list_goods_model_color
										}">{{$zhTran('型号：')}}{{ goodsItem.goods_model == null ? '' : goodsItem.goods_model }}</view>
											<view class="m-t-10 flex row-between">
												<price v-if="orderItem.order_type == 0 || orderItem.order_type == 4"
													:content="goodsItem.original_price" mainSize="32rpx"
													minorSize="24rpx" :prefix="price_symbol" />
												<price v-else :content="goodsItem.goods_price" mainSize="32rpx"
													minorSize="24rpx" :prefix="price_symbol" />
												<text class="lighter sm">x{{ goodsItem.goods_num }}</text>
											</view>
										</view>
									</goods-card>
									<block v-if="orderItem.pay_way == 5">
										<view class="order-desc">
											<view>
												<text>{{$zhTran('共 '+orderItem.total_num +' 件商品,订单总金额:')}}</text>
												<price class="normal lg" :content="orderItem.order_amount"
													:prefix="price_symbol" />
											</view>
											<view>
												<text>{{$zhTran('分润金额:')}}</text>
												<price class="normal lg" :content="orderItem.profit"
													:prefix="price_symbol" />
											</view>
										</view>
										<view class="order-desc">
											<text>{{$zhTran('余额付款:')}}</text>
											<price class="normal lg" :content="orderItem.bal_amount"
												:prefix="price_symbol" />
										</view>
										<view class="order-desc">
											<text>{{ $zhTran(orderItem.pay_status == 2 ? '应付款:' : '微信付款：') }}</text>
											<price class="normal lg" :content="orderItem.need_amount"
												:prefix="price_symbol" />
										</view>
									</block>
									<block v-else>
										<view class="order-desc" v-if="orderItem.integrate_amount > 0">
											<view>
												<text>{{$zhTran('共 '+ orderItem.total_num +' 件商品,已付款:')}}</text>
												<price class="normal lg" :content="orderItem.done_amount"
													:prefix="price_symbol" />
												<text>{{$zhTran(',应付款:')}}</text>
												<price class="normal lg" :content="orderItem.integrate_amount"
													:prefix="price_symbol" />
											</view>
											<view>
												<text>{{$zhTran('分润金额:')}}</text>
												<price class="normal lg" :content="orderItem.profit"
													:prefix="price_symbol" />
											</view>
										</view>
										<view class="order-desc" v-else>
											<view>
												<text>{{$zhTran('共 '+ orderItem.total_num +' 件商品,应付款:')}}</text>
												<price class="normal lg" :content="orderItem.order_amount"
													:prefix="price_symbol" />
											</view>
											<view>
												<text>{{$zhTran('分润金额:')}}</text>
												<price class="normal lg" :content="orderItem.profit"
													:prefix="price_symbol" />
											</view>
										</view>
									</block>
								</view>

								<!-- Order Footer -->
								<view class="order-footer">
									<view>下单客户: {{orderItem.user.nickname}}</view>
									<view class="flex" v-if="orderItem.buttons.length">
										<block v-for="buttonItem in orderItem.buttons" :key="buttonItem.event">
											<view :class="[
											'order-button',
											`order-button--${buttonItem.style}`
										]" @tap="onOrderButtons(buttonItem['event'], orderItem.id, orderItem.user_id)">{{ buttonItem.name }}</view>
										</block>
										<block v-if="!orderItem.audit_status && orderItem.audit_show">
											<view :class="[
											'order-button', 
											`order-button--primary`
										]">{{ orderItem.audit_status_desc }}</view>
										</block>
									</view>
								</view>
							</view>
						</view>
					</block>
				</mescroll-uni>
			</view>
			<w-jishuzhichi :class="[{'jishuzhichi-nodata':currentTabList.length == 0}]" />
		</view>
	</view>
</template>


<script>
	import {
		apiCloudShopOrderList
	} from '@/api/cloudshop'
	import OrderMixin from '@/mixins/order'
	import {
		PaymentStatusEnum
	} from '@/utils/enum'
	import Cache from '@/utils/cache'
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins";
	export default {
		name: 'OrderList',
		mixins: [OrderMixin, MescrollMixin],

		data() {
			return {
				// Tabs列表
				tabsList: [{
					sign: 'all',
					name: this.$zhTran('全部'),
					list: [],
				}, {
					sign: 'pay',
					name: this.$zhTran('待付款'),
					list: [],
				}, {
					sign: 'delivery',
					name: this.$zhTran('待发货'),
					list: [],
				}, {
					sign: 'take',
					name: this.$zhTran('待收货'),
					list: [],
				}, {
					sign: 'finish',
					name: this.$zhTran('已确认'),
					list: [],
				}],
				tabsIndex: '', // Tabs索引

				// 订单状态
				orderStatus: {
					0: {
						name: this.$zhTran('待付款'),
						style: 'primary',
					},
					1: {
						name: this.$zhTran('待发货'),
						style: 'primary',
					},
					2: {
						name: this.$zhTran('待收货'),
						style: 'primary',
					},
					3: {
						name: this.$zhTran('已确认'),
						style: 'primary',
					},
					4: {
						name: this.$zhTran('已关闭'),
						style: 'muted',
					}
				},

				price_symbol: '￥',
				order_extend: {
					show_order_list_goods_code: 0,
					order_list_goods_code_color: '',
					show_order_list_goods_model: 0,
					order_list_goods_model_color: '',
					show_order_list_goods_tag: 0
				},
			}
		},

		computed: {
			// 当前Tab项
			currentTab() {
				return this.tabsList[this.tabsIndex] || {}
			},

			currentTabList() {
				return this.tabsList[this.tabsIndex].list || []
			},

			// 映射Tabs项
			mapTabsItem() {
				return sign => this.tabsList.find(item => item['sign'] === sign)
			}
		},

		methods: {
			// 更改当前Tab页
			changeCurrentTab(index) {
				this.tabsIndex = index
				this.refreshOrderData()
			},

			// 刷新订单数据 @Mixins【/src/mixins/order.js】内调用，请勿删除
			refreshOrderData() {
				this.$nextTick(() => {
					this.mescroll.resetUpScroll()
				})
			},
			// 上拉加载更多
			upCallback({
				num,
				size
			}) {
				const sign = this.currentTab.sign
				apiCloudShopOrderList({
					type: sign,
					page_no: num,
					page_size: size,
				}).then(({
					lists,
					page_size,
					count,
					extend
				}) => {
					const tab = this.mapTabsItem(sign)
					lists.forEach(item => item['buttons'] = this.getOrderStatusButtons(item.btn) ?? [])
					if (num === 1) tab.list = []
					tab.list = [...tab.list, ...lists]
					tab.list = this.$zhTran(tab.list)
					this.price_symbol = extend.price_symbol
					this.order_extend = extend
					this.mescroll.endSuccess(lists.length, count)
				}).catch(err => {
					this.mescroll.endErr()
				})
			},

			// 去订单详情
			goOrderDetail(row) {
				this.$Router.push({
					path: '/bundle2/pages/cloud_shop/order/detail',
					query: {
						order_id: row.id,
						order_user: row.user_id
					}
				})
			},
		},

		onLoad() {
			const options = this.$Route.query
			const mode = options.mode ?? 'all'
			const tabIndex = this.tabsList.findIndex(item => item['sign'] === mode)

			this.changeCurrentTab(tabIndex)
			setTimeout(() => {}, 0.5 * 1000)
		},
		onUnload() {},
	}
</script>


<style lang="scss">
	page {
		height: 100%;
		padding: 0;
	}

	.order-list {
		height: 100%;
		display: flex;
		flex-direction: column;

		.order-item {
			flex: 1;
			min-height: 0;

			.order-item-box {
				height: calc(100% - 80rpx - constant(safe-area-inset-bottom));
				height: calc(100% - 80rpx - env(safe-area-inset-bottom));
			}
		}
	}

	.order-contain {
		padding: 0 20rpx;
	}

	.order {
		padding-left: 20rpx;
		margin-top: 20rpx;
		border-radius: 5px;
		background-color: #FFFFFF;

		&-header {
			// display: flex;
			height: 80rpx;
			align-items: center;
			padding-right: 20rpx;
			border-bottom: $-solid-border;

			.order-sn {
				font-size: $-font-size-nr;
			}

			.order-status {
				margin-left: auto;
				font-size: $-font-size-sm;

				&--primary {
					@include font_color()
				}

				&--muted {
					color: $-color-muted;
				}
			}
		}

		&-main {
			.order-desc {
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 20rpx;
				font-size: $-font-size-xs;
				color: $-color-muted;
			}
		}

		&-footer {
			display: flex;
			justify-content: space-between;
			align-items: center;
			height: 100rpx;
			padding-right: 20rpx;
			border-top: $-solid-border;

			.order-button {
				box-sizing: border-box;
				padding: 10rpx 30rpx;
				border-radius: 2 px;
				font-size: $-font-size-nr;

				&:nth-child(n+2) {
					margin-left: 20rpx;
				}

				&--primary {
					background-color: rgba(64, 115, 250, 1);
					color: #FFFFFF;
				}

				&--normal {
					border-width: 1px;
					border-style: solid;
					@include border_color() @include font_color()
				}

				&--muted {
					border: $-solid-border;
					color: $-color-lighter;
				}
			}
		}

	}
</style>